package flexo.gui;

import flexo.math.Coords;
import flexo.math.Vector;
import flexo.scene.Node;

public abstract class Cursor extends flexo.scene.ExclusiveEntity
{
	protected Coords size;
	protected flexo.scene.Node hotspotNode;
	
	protected flexo.input.entity.Cursor input;
	
	public Cursor(Coords size, Coords hotspot)
	{
		super(flexo.scene.Entity.Type.OTHER);
		this.size = size;
		flexo.input.InputSystem is = (flexo.input.InputSystem)flexo.Kernel.getSystem(flexo.input.InputSystem.ID);
		input = is.addCursor();
		input.setSensitivity(100);
		
		if (!is.existsAction(flexo.input.entity.Cursor.CURSOR_UP, flexo.input.EventType.MOUSE_Y_POS))
		{
			is.addAction(flexo.input.entity.Cursor.CURSOR_UP, flexo.input.EventType.MOUSE_Y_POS);
			is.addAction(flexo.input.entity.Cursor.CURSOR_DOWN, flexo.input.EventType.MOUSE_Y_NEG);
			is.addAction(flexo.input.entity.Cursor.CURSOR_LEFT, flexo.input.EventType.MOUSE_X_NEG);
			is.addAction(flexo.input.entity.Cursor.CURSOR_RIGHT, flexo.input.EventType.MOUSE_X_POS);
		}
	}
	
	public Coords getCoords()
	{
		Vector mouse = this.exclusiveNode.getPosition();
		return new Coords(mouse.x,mouse.y);
	}
	
	public void setSensitivity(float value)
	{
		this.input.setSensitivity(value);
	}
	
	@Override
	public boolean attachTo(Node n)
	{
		boolean result = super.attachTo(n);
		if (result)
		{
			hotspotNode = n.addChild("cursor");
			hotspotNode.setPosition(new Vector(0,0,0));
			input.attachTo(n);
		}
		return result;
	}
	
	@Override
	public void deattachFrom(Node n)
	{
		hotspotNode.remove();
		hotspotNode = null;
		input.deattachFrom(n);
		super.deattachFrom(n);		
	}
}
